//
//  AppDelegate+BlurryImage.m
//  test
//
//  Created by xxh on 2022/1/20.
//

#import "AppDelegate+BlurryImage.h"
#import <Accelerate/Accelerate.h>

#define kBlurryTag 10000

@implementation AppDelegate (BlurryImage)

//添加高斯模糊页面
- (void)showBlurryViewInWindow {
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    
    UIView *view = [[UIView alloc] initWithFrame:window.frame];
    
    UIImage *sourceImage = [self getCurrentImage];
    if (sourceImage) {
        UIImage *image = [UIImage imageWithData:UIImageJPEGRepresentation(sourceImage, 1.0)];
        UIImage *sImage =  [self blurryImage:image withBlurLevel:0.1];
        UIImageView *bgView = [[UIImageView alloc] initWithFrame:window.frame];
        bgView.image = sImage;
        [view addSubview:bgView];
    }
    
    view.tag = kBlurryTag;
    
    for (UIView *subView in [UIApplication sharedApplication].keyWindow.subviews) {
        if (subView.tag == kBlurryTag) {
            [subView removeFromSuperview];
        }
    }
    [[UIApplication sharedApplication].keyWindow addSubview:view];
    
}
//去除高斯模糊页面
- (void)removeBlurryViewInWindow {
    for (UIView *subView in [UIApplication sharedApplication].keyWindow.subviews) {
        if (subView.tag == kBlurryTag) {
            [subView removeFromSuperview];
        }
    }
}


//获取当前页面截图
- (UIImage *)getCurrentImage{
    
    UIView *sourceView = [self getCurrentVisibleView];
    
    if (sourceView == nil) {
        return nil;
    }
    
    UIGraphicsBeginImageContext(sourceView.bounds.size);
    [sourceView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

//获取当前view
- (UIView *)getCurrentVisibleView {
    UIWindow *window = [[UIApplication sharedApplication].delegate window];
    UIViewController *rootViewController = window.rootViewController;
    if ([rootViewController isKindOfClass:[UINavigationController class]]) {
        return ((UINavigationController *)rootViewController).visibleViewController.view;
    }
    return rootViewController.view;
}

//对图片进行高斯模糊
- (UIImage *)blurryImage:(UIImage *)image withBlurLevel:(CGFloat)blur {
    if (blur < 0.f || blur > 1.f) {
        blur = 0.5f;
    }
    int boxSize = (int)(blur * 100);
    boxSize = boxSize - (boxSize % 2) + 1;
    
    CGImageRef img = image.CGImage;
    
    vImage_Buffer inBuffer, outBuffer;
    vImage_Error error;
    
    void *pixelBuffer;
    
    CGDataProviderRef inProvider = CGImageGetDataProvider(img);
    CFDataRef inBitmapData = CGDataProviderCopyData(inProvider);
    
    inBuffer.width = CGImageGetWidth(img);
    inBuffer.height = CGImageGetHeight(img);
    inBuffer.rowBytes = CGImageGetBytesPerRow(img);
    
    inBuffer.data = (void*)CFDataGetBytePtr(inBitmapData);
    
    pixelBuffer = malloc(CGImageGetBytesPerRow(img) *
                         CGImageGetHeight(img));
    
    if(pixelBuffer == NULL)
        NSLog(@"No pixelbuffer");
    
    outBuffer.data = pixelBuffer;
    outBuffer.width = CGImageGetWidth(img);
    outBuffer.height = CGImageGetHeight(img);
    outBuffer.rowBytes = CGImageGetBytesPerRow(img);
    
    error = vImageBoxConvolve_ARGB8888(&inBuffer,
                                       &outBuffer,
                                       NULL,
                                       0,
                                       0,
                                       boxSize,
                                       boxSize,
                                       NULL,
                                       kvImageEdgeExtend);
    
    
    if (error) {
        NSLog(@"error from convolution %ld", error);
    }
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef ctx = CGBitmapContextCreate(
                                             outBuffer.data,
                                             outBuffer.width,
                                             outBuffer.height,
                                             8,
                                             outBuffer.rowBytes,
                                             colorSpace,
                                             kCGImageAlphaNoneSkipLast);
    CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
    UIImage *returnImage = [UIImage imageWithCGImage:imageRef];
    
    //clean up
    CGContextRelease(ctx);
    CGColorSpaceRelease(colorSpace);
    
    free(pixelBuffer);
    CFRelease(inBitmapData);
    
    CGColorSpaceRelease(colorSpace);
    CGImageRelease(imageRef);
    
    return returnImage;
}

@end
